#include "statusbarformatter.h"
#include <QLocale>

// Martin Fowler重构：Move Method - 格式化逻辑从StatusBarManager迁移

QString StatusBarFormatter::formatPageInfo(int currentPage, int totalPages)
{
    if (totalPages == 0) {
        return "第 0 页";
    }
    return QString("第 %1/%2 页").arg(currentPage).arg(totalPages);
}

QString StatusBarFormatter::formatCacheInfo(qint64 cacheSize, int cacheCount)
{
    QString sizeStr = formatFileSize(cacheSize);
    QString countStr = formatCount(cacheCount, "项");
    return QString("缓存: %1, %2").arg(sizeStr).arg(countStr);
}

QString StatusBarFormatter::formatExportSettings(int dpi, const QString& quality)
{
    return QString("DPI: %1, %2").arg(dpi).arg(quality);
}

QString StatusBarFormatter::formatFileSize(qint64 bytes)
{
    return formatBytesInternal(bytes, 1);
}

QString StatusBarFormatter::formatExportProgress(int completed, int total, double percentage)
{
    return QString("进度: %1/%2 (%3%)")
           .arg(completed)
           .arg(total)
           .arg(QString::number(percentage, 'f', 1));
}

QString StatusBarFormatter::formatElapsedTime(int seconds)
{
    if (seconds < 60) {
        return QString("%1秒").arg(seconds);
    } else if (seconds < 3600) {
        int minutes = seconds / 60;
        int remainingSeconds = seconds % 60;
        return QString("%1分%2秒").arg(minutes).arg(remainingSeconds);
    } else {
        int hours = seconds / 3600;
        int minutes = (seconds % 3600) / 60;
        return QString("%1小时%2分").arg(hours).arg(minutes);
    }
}

QString StatusBarFormatter::formatCount(int count, const QString& unit, const QString& pluralUnit)
{
    if (pluralUnit.isEmpty()) {
        return QString("%1 %2").arg(count).arg(unit);
    } else {
        return QString("%1 %2").arg(count).arg(pluralize(count, unit, pluralUnit));
    }
}

// 私有辅助方法实现

QString StatusBarFormatter::formatBytesInternal(qint64 bytes, int precision)
{
    // Martin Fowler重构：从StatusBarManager::formatFileSize移动而来
    constexpr qint64 KB = 1024;
    constexpr qint64 MB = KB * 1024;
    constexpr qint64 GB = MB * 1024;

    if (bytes >= GB) {
        return QString("%1 GB").arg(bytes / (double)GB, 0, 'f', precision);
    } else if (bytes >= MB) {
        return QString("%1 MB").arg(bytes / (double)MB, 0, 'f', precision);
    } else if (bytes >= KB) {
        return QString("%1 KB").arg(bytes / (double)KB, 0, 'f', 0);
    } else {
        return QString("%1 B").arg(bytes);
    }
}

QString StatusBarFormatter::pluralize(int count, const QString& singular, const QString& plural)
{
    return count == 1 ? singular : plural;
}